package com.lenovo.agricultureclient.ui.activity;

import java.util.ArrayList;

import com.lenovo.agricultureclient.AppConfig;
import com.lenovo.agricultureclient.R;
import com.lenovo.agricultureclient.bean.ChartPagerBean;
import com.lenovo.agricultureclient.bean.SensorConfig;
import com.lenovo.agricultureclient.bean.SensorValue;
import com.lenovo.agricultureclient.ui.adapter.ChartPagerAdapter;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 实时图表显示界面，其中包含了5个图表，分别是pm2.5、CO2、光照强度、空气温湿度和土壤温湿度5个图表
 * @author zongbingwu
 *
 */
public class RealTimeDataActivity extends AppBaseActivity implements OnPageChangeListener
{
	private static final String TAG = "<RealTimeDataActivity>";
	
	public static final String CHART_TYPE = "chart_type";
	//5个图表的id值
	public static final int PM25_CHART 	= 0x00;
	public static final int CO2_CHART 	= 0x01;
	public static final int LIGHT_CHART = 0x02;
	public static final int AIR_T_CHART 	= 0x03;
	public static final int AIR_H_CHART 	= 0x04;
	public static final int SOIL_T_CHART 	= 0x05;
	public static final int SOIL_H_CHART 	= 0x06;
	
	private TextView mTitleTextView;
	//图表容器view
	private ViewPager mChartPager;
	//图表view的适配器
	private ChartPagerAdapter mChartPagerAdp;
	//当前图表位置标记点
	private LinearLayout mTipsGroupView;
	
	//5个曲线图表信息对象
	private ArrayList<ChartPagerBean> mChartArray;
	private ChartPagerBean mPm25Chart;
	private ChartPagerBean mCo2Chart;
	private ChartPagerBean mLightChart;
	private ChartPagerBean mAirTChart;
	private ChartPagerBean mAirHChart;
	private ChartPagerBean mSoilTChart;
	private ChartPagerBean mSoilHChart;
	//默认显示的图表
	private int mPagerItem = PM25_CHART;
	//所有传感器的预警值范围
	private SensorConfig mSensorConfig;
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.real_time_data_activity);
    	mPagerItem = getIntent().getIntExtra(CHART_TYPE, mPagerItem);
    	mSensorConfig = mApp.getSensorConfig();
    	
    	//初始化5个曲线图表信息对象
    	initChartData();
    	//初始化控件
		initView();
		//刷新界面
		updateView();
		//开始不断的获取传感器数据
		startGetSensorData();
    }
    
    //初始化控件
	private void initView()
	{
		mTitleTextView = (TextView)findViewById(R.id.title_text_view);
		mTitleTextView.setText(R.string.pm25_real_data);
		
		mChartPager = (ViewPager)findViewById(R.id.chart_pager);
		mTipsGroupView = (LinearLayout)findViewById(R.id.viewGroup);
		setupTipsImage();
		
		mChartPagerAdp = new ChartPagerAdapter(this, mChartArray);
		mChartPager.setAdapter(mChartPagerAdp);
		mChartPager.setOnPageChangeListener(this);
		mChartPager.setCurrentItem(mPagerItem);
	}
	//初始化5个曲线图表信息对象
	private void initChartData()
	{
		mChartArray = new ArrayList<ChartPagerBean>();
		mPm25Chart = new ChartPagerBean(getString(R.string.pm25_title));
		mCo2Chart = new ChartPagerBean(getString(R.string.CO2_title));
		mLightChart = new ChartPagerBean(getString(R.string.light_title));
		mAirTChart = new ChartPagerBean(getString(R.string.air_tmper_title));
		mAirHChart = new ChartPagerBean(getString(R.string.air_humid_title));
		mSoilTChart = new ChartPagerBean(getString(R.string.soil_tmper_title));
		mSoilHChart = new ChartPagerBean(getString(R.string.soil_humid_title));
		
		mChartArray.add(mPm25Chart);
		mChartArray.add(mCo2Chart);
		mChartArray.add(mLightChart);
		mChartArray.add(mAirTChart);
		mChartArray.add(mAirHChart);
		mChartArray.add(mSoilTChart);
		mChartArray.add(mSoilHChart);
		
		mPm25Chart.majorMax = 500;//pm2.5的最大值
		mPm25Chart.majorMin = 0;//pm2.5的最小值
		mPm25Chart.majorWarningMin = -1000;//pm2.5的告警最小值
		mPm25Chart.majorWarningMax = mSensorConfig.maxPM25;//pm2.5的告警最大值
		
		mCo2Chart.majorMin = 0;//co2的最小值
		mCo2Chart.majorMax = 1000;//co2的最大值
		mCo2Chart.majorWarningMin = mSensorConfig.minCo2;//co2的告警最小值
		mCo2Chart.majorWarningMax = mSensorConfig.maxCo2;//co2的告警最大值
		
		mLightChart.majorMin = 0;//光照强度最小值
		mLightChart.majorMax = 15000;//光照强度最大值
		mLightChart.majorWarningMin = mSensorConfig.minLight;//光照强度的告警最小值
		mLightChart.majorWarningMax = mSensorConfig.maxLight;//光照强度的告警最大值
		
		mAirTChart.majorMin = 0;//空气温度最小值
		mAirTChart.majorMax = 100;//空气温度最大值
		mAirTChart.majorWarningMin = mSensorConfig.minAirTemperature;//空气温度的告警最小值
		mAirTChart.majorWarningMax = mSensorConfig.maxAirTemperature;//空气温度的告警最大值

		mAirHChart.majorMin = 0;////空气湿度最小值
		mAirHChart.majorMax = 100;//空气湿度最大值
		mAirHChart.majorWarningMin = mSensorConfig.minAirHumidity;//空气湿度的告警最小值
		mAirHChart.majorWarningMax = mSensorConfig.maxAirHumidity;//空气湿度的告警最大值
		
		mSoilTChart.majorMin = 0;//土壤温度最小值
		mSoilTChart.majorMax = 100;//土壤温度最大值
		mSoilTChart.majorWarningMin = mSensorConfig.minSoilTemperature;//土壤温度的告警最小值
		mSoilTChart.majorWarningMax = mSensorConfig.maxSoilTemperature;//土壤温度的告警最大值

		mSoilHChart.majorMin = 0;//土壤湿度最小值
		mSoilHChart.majorMax = 100;//土壤湿度最大值
		mSoilHChart.majorWarningMin = mSensorConfig.minSoilHumidity;//土壤湿度的告警最小值
		mSoilHChart.majorWarningMax = mSensorConfig.maxSoilHumidity;//土壤湿度的告警最大值
	}
	
	private void updateView()
	{
		//获取当前传感器的值
		SensorValue sensor = mApp.getCurSensorValue();
		if(sensor != null)
		{
			//为各个图表添加新数据
			mPm25Chart.majorValueList.add(sensor.getPm25());
			mCo2Chart.majorValueList.add(sensor.getCo2());
			mLightChart.majorValueList.add(sensor.getLight());
			mAirTChart.majorValueList.add(sensor.getAirTemper());
			mAirHChart.majorValueList.add(sensor.getAirHumid());
			mSoilTChart.majorValueList.add(sensor.getSoilTemper());
			mSoilHChart.majorValueList.add(sensor.getSoilHumid());
			
			//如果数据点过多，则清除最老数据
			if(mPm25Chart.majorValueList.size()>AppConfig.CHART_MAX_POINT){
				mPm25Chart.majorValueList.poll();
			}
			if(mCo2Chart.majorValueList.size()>AppConfig.CHART_MAX_POINT){
				mCo2Chart.majorValueList.poll();
			}
			if(mLightChart.majorValueList.size()>AppConfig.CHART_MAX_POINT){
				mLightChart.majorValueList.poll();
			}
			if(mAirTChart.majorValueList.size()>AppConfig.CHART_MAX_POINT){
				mAirTChart.majorValueList.poll();
			}
			if(mAirHChart.majorValueList.size()>AppConfig.CHART_MAX_POINT){
				mAirHChart.majorValueList.poll();
			}
			if(mSoilTChart.majorValueList.size()>AppConfig.CHART_MAX_POINT){
				mSoilTChart.majorValueList.poll();
			}
			if(mSoilHChart.majorValueList.size()>AppConfig.CHART_MAX_POINT){
				mSoilHChart.majorValueList.poll();
			}
			//更新图表
			mChartPagerAdp.notifyDataSetChanged();
		}
	}
	
	@Override
	protected void onGetSensor() 
	{
		updateView();
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	//更换界面标题
	@Override
	public void onPageSelected(int arg0) {
		setTipImageSelect(arg0);
		int titleResid[] = {R.string.pm25_real_data,R.string.co2_real_data,R.string.light_real_data,
							R.string.air_t_real_data,R.string.air_h_real_data, 
							R.string.soil_t_real_data,R.string.soil_h_real_data};
		if(arg0>=0 && arg0<7){
			mTitleTextView.setText(titleResid[arg0]);
		}
	} 
	
	//构建图表位置标记点
	private void setupTipsImage()
    {
    	mTipsGroupView.removeAllViews();
	    for(int i=0; i<mChartArray.size(); i++)
	    {  
	        ImageView imageView = new ImageView(this);  
	        if(i == 0){  
	        	imageView.setBackgroundResource(R.drawable.page_indicator_focused);  
	        }else{  
	        	imageView.setBackgroundResource(R.drawable.page_indicator_unfocused);  
	        }
	        imageView.setLayoutParams(new LayoutParams(5,5)); 
	          
	        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(30, 30));  
	        layoutParams.leftMargin = 5;  
	        layoutParams.rightMargin = 5;  
	        mTipsGroupView.addView(imageView, layoutParams);  
	    }
    }
	//根据当前的图表位置来高亮相应的标记点
	private void setTipImageSelect(int selectItems){
        for(int i=0; i<mTipsGroupView.getChildCount(); i++)
        {  
        	View childView = mTipsGroupView.getChildAt(i);
            if(i == selectItems){  
            	childView.setBackgroundResource(R.drawable.page_indicator_focused);  
            }else{  
            	childView.setBackgroundResource(R.drawable.page_indicator_unfocused);  
            }  
        }  
    }

	@Override
	protected void onDestroy() {
		super.onDestroy();
		System.gc();
	}
}
